//index.js
//获取应用实例
var app = getApp();
var gData = app.globalData
var x, y, x1, y1, x2, y2, index, currindex, n, yy;
var arr1;
//var arr1 = [{ content: 11, id: 1 }, { content: 22, id: 2 }, { content: 33, id: 3 }, { content: 44, id: 4 }, { content: 55, id: 5 }];
Page({
   data: {
      mainx: 0,
      //content: [{ content: 11, id: 1 }, { content: 22, id: 2 }, { content: 33, id: 3 }, { content: 44, id: 4 }, { content: 55, id: 5 }],
      content: '',
      start: { x: 0, y: 0 }
   },
   onLoad: function () {
      var list = gData.tableCateArr
      arr1 = []
      for (var i = 0; i < list.length; i++) {
         var obj = list[i]
         obj.index = i + 1
         arr1.push(obj)
      }

      this.setData({ content: arr1 })
      console.log('arr1===', arr1, 'this.data', this.data)
   },
   movestart: function (e) {
      console.log('movestart: function (e', e)
      currindex = e.target.dataset.index;
      x = e.touches[0].clientX;
      y = e.touches[0].clientY;
      x1 = e.currentTarget.offsetLeft;
      y1 = e.currentTarget.offsetTop;
   },
   move: function (e) {
      yy = e.currentTarget.offsetTop;
      x2 = e.touches[0].clientX - x + x1;
      y2 = e.touches[0].clientY - y + y1;
      this.setData({
         mainx: currindex,
         opacity: 0.7,
         start: { x: x2, y: y2 }
      })
   },
   moveend: function () {
      if (y2 != 0) {

         var nx = this.data.content.length;
         var arr = [];
         for (var i = 0; i < nx; i++) {
            arr.push(this.data.content[i]);
         }

         n = 1;
         for (var k = 2; k < nx; k++) {
            if (y2 > (52 * (k - 1) + k * 2 - 26)) {
               n = k;
            }
         }
         if (y2 > (52 * (nx - 1) + nx * 2 - 26)) {
            n = nx;
         }

         for (var m = 0; m < nx; m++) {
            console.log('交换前arr[m]======', arr[m]);
            console.log('交换前arr1[m]======', arr1[m]);
            console.log('交换前arr[' + m + '].index==' + arr[m].index + '--' + arr1[m].index + 'arr1[' + m + '].index');
         }

         arr.splice((currindex - 1), 1);
         arr.splice((n - 1), 0, arr1[currindex - 1]);



         for (var m = 0; m < nx; m++) {
            console.log('交换中arr[m]======', arr[m]);
            console.log('交换中arr1[m]======', arr1[m]);
            console.log('交换中arr[' + m + '].index==' + arr[m].index + '--' + arr1[m].index + 'arr1[' + m + '].index');
         }
         // arr1 = arr
         // arr1 = [];
         for (var m = 0; m < nx; m++) {
            console.log('++++@@@arr[m]======', arr[m]);
            arr[m].index = m + 1;
            arr1.push(arr[m]);
            console.log('++++@@@arr1[m]======', arr1[m]);
         }
         for (var m = 0; m < nx; m++) {
            console.log('交换后arr[m]======', arr[m]);
            console.log('交换后arr1[m]======', arr1[m]);
            console.log('交换后arr[' + m + '].index==' + arr[m].index + '--' + arr1[m].index + 'arr1[' + m + '].index');
         }
         this.setData({
            mainx: 0,
            content: arr,
            opacity: 1
         })
      }
   }
})